﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SysUser
    {
        public int Add(Chain.Model.SysUser model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysUser(");
            builder.Append("UserAccount,UserName,UserPassword,UserShopID,UserGroupID,UserLock,UserRemark,UserCreateTime,UserTelephone,UserNumber)");
            builder.Append(" values (");
            builder.Append("@UserAccount,@UserName,@UserPassword,@UserShopID,@UserGroupID,@UserLock,@UserRemark,@UserCreateTime,@UserTelephone,@UserNumber)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserAccount", SqlDbType.VarChar, 200), new SqlParameter("@UserName", SqlDbType.NVarChar, 50), new SqlParameter("@UserPassword", SqlDbType.VarChar, 500), new SqlParameter("@UserShopID", SqlDbType.Int, 4), new SqlParameter("@UserGroupID", SqlDbType.Int, 4), new SqlParameter("@UserLock", SqlDbType.Bit, 1), new SqlParameter("@UserRemark", SqlDbType.NVarChar, 500), new SqlParameter("@UserCreateTime", SqlDbType.DateTime), new SqlParameter("@UserTelephone", SqlDbType.VarChar, 50), new SqlParameter("@UserNumber", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = model.UserAccount;
            cmdParms[1].Value = model.UserName;
            cmdParms[2].Value = model.UserPassword;
            cmdParms[3].Value = model.UserShopID;
            cmdParms[4].Value = model.UserGroupID;
            cmdParms[5].Value = model.UserLock;
            cmdParms[6].Value = model.UserRemark;
            cmdParms[7].Value = model.UserCreateTime;
            cmdParms[8].Value = model.UserTelephone;
            cmdParms[9].Value = model.UserNumber;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataTable CheckUserLogin(string UserAccount, string Md5Pwd)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 UserID,UserAccount,UserName,UserPassword,UserShopID,UserGroupID,UserLock,UserRemark,UserCreateTime,UserTelephone,UserNumber from SysUser ");
            builder.Append(" where UserAccount=@UserAccount and UserPassword=@UserPassword");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserAccount", SqlDbType.VarChar, 50), new SqlParameter("@UserPassword", SqlDbType.VarChar, 200) };
            cmdParms[0].Value = UserAccount;
            cmdParms[1].Value = Md5Pwd;
            return DbHelperSQL.Query(builder.ToString(), cmdParms).Tables[0];
        }

        public bool Delete(int UserID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysUser ");
            builder.Append(" where UserID=@UserID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4) };
            cmdParms[0].Value = UserID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string UserIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysUser ");
            builder.Append(" where UserID in (" + UserIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool ExiNumber(string usernumber)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysUser");
            builder.Append(" where UserNumber=@UserNumber");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserNumber", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = usernumber;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public int ExistPwd(int UserID, string oldPwd)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("select UserPassword from SysUser where UserID={0}", UserID);
            if (DbHelperSQL.GetSingle(builder.ToString()).ToString() != oldPwd)
            {
                return -1;
            }
            return 1;
        }

        public bool Exists(string useraccount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysUser");
            builder.Append(" where UserAccount=@UserAccount");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserAccount", SqlDbType.VarChar, 200) };
            cmdParms[0].Value = useraccount;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(string useraccount, int userid)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysUser");
            builder.Append(" where UserAccount = @UserAccount and UserID<>@UserID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserAccount", SqlDbType.VarChar, 200), new SqlParameter("@UserID", SqlDbType.Int) };
            cmdParms[0].Value = useraccount;
            cmdParms[1].Value = userid;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(string useraccount, string usernumber)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysUser");
            builder.Append(" where UserAccount=@UserAccount or UserNumber=@UserNumber");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserAccount", SqlDbType.VarChar, 200), new SqlParameter("@UserNumber", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = useraccount;
            cmdParms[1].Value = usernumber;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetIsCanDelUser(int userid)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int) };
            parameters[0].Value = userid;
            return DbHelperSQL.RunProcedure("UserIsCanDel", parameters, "#UserIsCanDel");
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select UserID,UserAccount,UserName,UserPassword,UserShopID,UserGroupID,UserLock,UserRemark,UserCreateTime,UserTelephone,UserNumber ");
            builder.Append(" FROM SysUser ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" UserID,UserAccount,UserName,UserPassword,UserShopID,UserGroupID,UserLock,UserRemark,UserCreateTime,UserTelephone,UserNumber ");
            builder.Append(" FROM SysUser ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int PageSize, int PageIndex, string[] where, out int resCount)
        {
            string tableName = "SysUser";
            string[] column = new string[] { "*" };
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, where, "UserID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.UserID desc");
            }
            builder.Append(")AS Row, T.*  from SysUser T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "SysUser,SysShop,SysGroup";
            string[] column = new string[] { "SysUser.*,SysShop.ShopName,SysGroup.GroupName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "UserID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("UserID", "SysUser");
        }

        public Chain.Model.SysUser GetModel(int UserID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 UserID,UserAccount,UserName,UserPassword,UserShopID,UserGroupID,UserLock,UserRemark,UserCreateTime,UserTelephone,UserNumber from SysUser ");
            builder.Append(" where UserID=@UserID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4) };
            cmdParms[0].Value = UserID;
            Chain.Model.SysUser user = new Chain.Model.SysUser();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["UserID"] != null) && (set.Tables[0].Rows[0]["UserID"].ToString() != ""))
            {
                user.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["UserAccount"] != null) && (set.Tables[0].Rows[0]["UserAccount"].ToString() != ""))
            {
                user.UserAccount = set.Tables[0].Rows[0]["UserAccount"].ToString();
            }
            if ((set.Tables[0].Rows[0]["UserName"] != null) && (set.Tables[0].Rows[0]["UserName"].ToString() != ""))
            {
                user.UserName = set.Tables[0].Rows[0]["UserName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["UserPassword"] != null) && (set.Tables[0].Rows[0]["UserPassword"].ToString() != ""))
            {
                user.UserPassword = set.Tables[0].Rows[0]["UserPassword"].ToString();
            }
            if ((set.Tables[0].Rows[0]["UserShopID"] != null) && (set.Tables[0].Rows[0]["UserShopID"].ToString() != ""))
            {
                user.UserShopID = int.Parse(set.Tables[0].Rows[0]["UserShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["UserGroupID"] != null) && (set.Tables[0].Rows[0]["UserGroupID"].ToString() != ""))
            {
                user.UserGroupID = int.Parse(set.Tables[0].Rows[0]["UserGroupID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["UserLock"] != null) && (set.Tables[0].Rows[0]["UserLock"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["UserLock"].ToString() == "1") || (set.Tables[0].Rows[0]["UserLock"].ToString().ToLower() == "true"))
                {
                    user.UserLock = true;
                }
                else
                {
                    user.UserLock = false;
                }
            }
            if ((set.Tables[0].Rows[0]["UserRemark"] != null) && (set.Tables[0].Rows[0]["UserRemark"].ToString() != ""))
            {
                user.UserRemark = set.Tables[0].Rows[0]["UserRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["UserCreateTime"] != null) && (set.Tables[0].Rows[0]["UserCreateTime"].ToString() != ""))
            {
                user.UserCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["UserCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["UserTelephone"] != null) && (set.Tables[0].Rows[0]["UserTelephone"].ToString() != ""))
            {
                user.UserTelephone = set.Tables[0].Rows[0]["UserTelephone"].ToString();
            }
            if ((set.Tables[0].Rows[0]["UserNumber"] != null) && (set.Tables[0].Rows[0]["UserNumber"].ToString() != ""))
            {
                user.UserNumber = set.Tables[0].Rows[0]["UserNumber"].ToString();
            }
            return user;
        }

        public Chain.Model.SysUser GetModel(string Account)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 UserID,UserAccount,UserName,UserPassword,UserShopID,UserGroupID,UserLock,UserRemark,UserCreateTime,UserTelephone,UserNumber from SysUser ");
            builder.Append(" where UserAccount=@UserAccount");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserAccount", SqlDbType.VarChar, 0xff) };
            cmdParms[0].Value = Account;
            Chain.Model.SysUser user = new Chain.Model.SysUser();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["UserID"] != null) && (set.Tables[0].Rows[0]["UserID"].ToString() != ""))
            {
                user.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["UserAccount"] != null) && (set.Tables[0].Rows[0]["UserAccount"].ToString() != ""))
            {
                user.UserAccount = set.Tables[0].Rows[0]["UserAccount"].ToString();
            }
            if ((set.Tables[0].Rows[0]["UserName"] != null) && (set.Tables[0].Rows[0]["UserName"].ToString() != ""))
            {
                user.UserName = set.Tables[0].Rows[0]["UserName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["UserPassword"] != null) && (set.Tables[0].Rows[0]["UserPassword"].ToString() != ""))
            {
                user.UserPassword = set.Tables[0].Rows[0]["UserPassword"].ToString();
            }
            if ((set.Tables[0].Rows[0]["UserShopID"] != null) && (set.Tables[0].Rows[0]["UserShopID"].ToString() != ""))
            {
                user.UserShopID = int.Parse(set.Tables[0].Rows[0]["UserShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["UserGroupID"] != null) && (set.Tables[0].Rows[0]["UserGroupID"].ToString() != ""))
            {
                user.UserGroupID = int.Parse(set.Tables[0].Rows[0]["UserGroupID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["UserLock"] != null) && (set.Tables[0].Rows[0]["UserLock"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["UserLock"].ToString() == "1") || (set.Tables[0].Rows[0]["UserLock"].ToString().ToLower() == "true"))
                {
                    user.UserLock = true;
                }
                else
                {
                    user.UserLock = false;
                }
            }
            if ((set.Tables[0].Rows[0]["UserRemark"] != null) && (set.Tables[0].Rows[0]["UserRemark"].ToString() != ""))
            {
                user.UserRemark = set.Tables[0].Rows[0]["UserRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["UserCreateTime"] != null) && (set.Tables[0].Rows[0]["UserCreateTime"].ToString() != ""))
            {
                user.UserCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["UserCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["UserTelephone"] != null) && (set.Tables[0].Rows[0]["UserTelephone"].ToString() != ""))
            {
                user.UserTelephone = set.Tables[0].Rows[0]["UserTelephone"].ToString();
            }
            if ((set.Tables[0].Rows[0]["UserNumber"] != null) && (set.Tables[0].Rows[0]["UserNumber"].ToString() != ""))
            {
                user.UserNumber = set.Tables[0].Rows[0]["UserNumber"].ToString();
            }
            return user;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysUser ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public string GetUserNameByUserID(string userid)
        {
            string sQLString = "SELECT UserName FROM SysUser WHERE UserID = @UserID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4) };
            cmdParms[0].Value = userid;
            try
            {
                return DbHelperSQL.GetSingle(sQLString, cmdParms).ToString();
            }
            catch
            {
                return "无此会员";
            }
        }

        public int Update(Chain.Model.SysUser model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysUser set ");
            builder.Append("UserAccount=@UserAccount,");
            builder.Append("UserName=@UserName,");
            builder.Append("UserPassword=@UserPassword,");
            builder.Append("UserShopID=@UserShopID,");
            builder.Append("UserGroupID=@UserGroupID,");
            builder.Append("UserLock=@UserLock,");
            builder.Append("UserRemark=@UserRemark,");
            builder.Append("UserCreateTime=@UserCreateTime,");
            builder.Append("UserTelephone=@UserTelephone,");
            builder.Append("UserNumber=@UserNumber");
            builder.Append(" where UserID=@UserID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserAccount", SqlDbType.VarChar, 200), new SqlParameter("@UserName", SqlDbType.NVarChar, 50), new SqlParameter("@UserPassword", SqlDbType.VarChar, 500), new SqlParameter("@UserShopID", SqlDbType.Int, 4), new SqlParameter("@UserGroupID", SqlDbType.Int, 4), new SqlParameter("@UserLock", SqlDbType.Bit, 1), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@UserRemark", SqlDbType.NVarChar, 500), new SqlParameter("@UserCreateTime", SqlDbType.DateTime), new SqlParameter("@UserTelephone", SqlDbType.VarChar, 50), new SqlParameter("@UserNumber", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = model.UserAccount;
            cmdParms[1].Value = model.UserName;
            cmdParms[2].Value = model.UserPassword;
            cmdParms[3].Value = model.UserShopID;
            cmdParms[4].Value = model.UserGroupID;
            cmdParms[5].Value = model.UserLock;
            cmdParms[6].Value = model.UserID;
            cmdParms[7].Value = model.UserRemark;
            cmdParms[8].Value = model.UserCreateTime;
            cmdParms[9].Value = model.UserTelephone;
            cmdParms[10].Value = model.UserNumber;
            if (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0)
            {
                return 1;
            }
            return -3;
        }

        public int UpdateUserLock(int ShopID, int type)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" update SysUser set UserLock=@UserLock ");
            builder.Append(" where UserShopID=@UserShopID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserLock", SqlDbType.Bit, 1), new SqlParameter("@UserShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = type;
            cmdParms[1].Value = ShopID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }

        public int UpdateUserPwd(int UsreID, string newPwd)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" update SysUser set UserPassword=@UserPassword ");
            builder.Append(" where UserID=@UserID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@UserPassword", SqlDbType.VarChar, 500), new SqlParameter("@UserID", SqlDbType.Int, 4) };
            cmdParms[0].Value = newPwd;
            cmdParms[1].Value = UsreID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }
    }
}

